package com.hns.can.energy.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;

/**
 * 车辆能耗service
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Service
public class CarEnergyService extends CommonService {
    
    @Autowired
    private FillCodeDscValue codeDscValue;
    
    /**
     * 车辆能耗查询
     * @param vo
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @LeftOrgAnnotation()
    @Transactional
    public Pagination searchCarEnergy(NewEnergy vo) throws Exception {
    	Pagination page = null;
    	if("7".equals(vo.getEnergyTimeType())){
    		page = this.myBatisPage("carEnergy.searchCarEnergyMonCount", "carEnergy.searchCarEnergyMon", vo, CommEnerConsStatInfoEntity.class);
    	}else{
    		page = this.myBatisPage("carEnergy.searchCarEnergyCount", "carEnergy.searchCarEnergy", vo, CommEnerConsStatInfoEntity.class);
    	}
        List list = page.getData();
        if(PubUtil.isNotEmpty(list)){
        	fillCarEnergy(list);
        }
        return page;
    }
    
    /**
     * 导出
     * @param vo
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @LeftOrgAnnotation()
    @Transactional
    @SuppressWarnings({"unchecked" })
    public List<CommEnerConsStatInfoEntity> exportCarEnergy(NewEnergy vo) throws Exception {
    	List list = null;
    	if("7".equals(vo.getEnergyTimeType())){
    		list = PubUtil.convertListMapToObject(sqlMapDao.selectList("carEnergy.searchCarEnergyMon", vo), CommEnerConsStatInfoEntity.class);
    	}else{
    		list = PubUtil.convertListMapToObject(sqlMapDao.selectList("carEnergy.searchCarEnergy", vo), CommEnerConsStatInfoEntity.class);
    	}
        if(PubUtil.isNotEmpty(list)){
        	fillCarEnergy(list);
        }
        return list;
    }

    private void fillCarEnergy(List list) throws Exception {
        //转化生成线路名称
        Map<String,String> map = new HashMap<String,String>();
        map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fileVo1 = new FillParamsVo("lineId", map);
        
        //转化生成机构名称
        map = new HashMap<String,String>();
        map.put("organName",  GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fileVo2 = new FillParamsVo("organId", map);
        
        //转化生成车牌号、车辆类别、车辆、燃料类型
        map= new HashMap<String,String>();
        map.put("licPltNo",  GlobalConst.DEFAULT_CACHE_VALUE);
        map.put("carType", "carType");
        map.put("engiNo", "engiNo");
        
        //转化生成驾驶员名称
        FillParamsVo fileVo3 = new FillParamsVo("carId", map);
        map = new HashMap<String,String>();
        map.put("drvName",  GlobalConst.DEFAULT_CACHE_VALUE);
        FillParamsVo fileVo4 = new FillParamsVo("drvId", map);
        
        codeDscValue.fillInfoValue(list,new String[] {GlobalConst.CACHE_KEY_BASE_LINE_INFO, 
            GlobalConst.CACHE_KEY_BASE_DRV_INFO, GlobalConst.CACHE_KEY_BASE_ORGAN, 
            GlobalConst.CACHE_KEY_BASE_CAR_INFO}, fileVo1, fileVo2, fileVo3, fileVo4);
        Map<String, String> mapTmp = new HashMap<String, String>();
        mapTmp.put("carType", "CAT00000");
        codeDscValue.fillCodeDscValue(list, mapTmp);
    }
}
